<ion-header>
    <ion-toolbar>
        <ion-buttons slot="start">
            <ion-back-button [text]="'core.back' | translate" />
        </ion-buttons>
        <ion-title>
            <h1>
                <core-format-text *ngIf="title" [text]="title" contextLevel="module" [contextInstanceId]="module.id"
                    [courseId]="courseId" />
            </h1>
        </ion-title>
        <ion-buttons slot="end" [hidden]="!loaded">
            <ion-button *ngIf="assessmentId && access.assessingallowed" fill="clear" (click)="saveAssessment()">
                {{ 'core.save' | translate }}
            </ion-button>
            @if (canAddFeedback && !loadFeedbackToEditErrorMessage) {
                <ion-button fill="clear" (click)="saveEvaluation()">
                    {{ 'core.save' | translate }}
                </ion-button>
            }
        </ion-buttons>
    </ion-toolbar>
</ion-header>
<ion-content class="limited-width">
    <ion-refresher slot="fixed" [disabled]="!loaded" (ionRefresh)="refreshSubmission($event.target)"
        *ngIf="!((assessmentId && access.assessingallowed) || canAddFeedback)">
        <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}" />
    </ion-refresher>
    <core-loading [hideUntil]="loaded">
        <ion-list *ngIf="submission">
            <addon-mod-workshop-submission [submission]="submission" [courseId]="courseId" [module]="module" [workshop]="workshop"
                [access]="access" [submissionPage]="true" />
            <ion-item class="ion-text-wrap" *ngIf="canEdit || canDelete">
                <ion-label>
                    <ion-button expand="block" *ngIf="canEdit" (click)="editSubmission()">
                        <ion-icon name="fas-pen" slot="start" aria-hidden="true" />
                        {{ 'addon.mod_workshop.editsubmission' | translate }}
                    </ion-button>
                    <ion-button expand="block" *ngIf="!submission.deleted && canDelete" color="danger" (click)="deleteSubmission()">
                        <ion-icon name="fas-trash" slot="start" aria-hidden="true" />
                        {{ 'addon.mod_workshop.deletesubmission' | translate }}
                    </ion-button>
                    <ion-button expand="block" fill="outline" *ngIf="submission.deleted && canDelete" color="danger"
                        (click)="undoDeleteSubmission()">
                        <ion-icon name="fas-rotate-left" slot="start" aria-hidden="true" />
                        {{ 'core.restore' | translate }}
                    </ion-button>
                </ion-label>
            </ion-item>
        </ion-list>

        <ion-list *ngIf="!canAddFeedback && evaluate?.text">
            <ion-item class="ion-text-wrap">
                <core-user-avatar *ngIf="evaluateByProfile" [user]="evaluateByProfile" slot="start" [courseId]="courseId"
                    [userId]="evaluateByProfile.id" />
                <ion-label>
                    <h2 *ngIf="evaluateByProfile && evaluateByProfile.fullname">
                        {{ 'addon.mod_workshop.feedbackby' | translate : {$a: evaluateByProfile.fullname} }}
                    </h2>
                    <core-format-text [text]="evaluate?.text" contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId" />
                </ion-label>
            </ion-item>
        </ion-list>

        <ion-list *ngIf="ownAssessment && !assessment">
            <ion-item-divider class="ion-text-wrap">
                <ion-label>
                    <h2>{{ 'addon.mod_workshop.yourassessment' | translate }}</h2>
                </ion-label>
            </ion-item-divider>
            <addon-mod-workshop-assessment [submission]="submission" [assessment]="ownAssessment" [courseId]="courseId" [access]="access"
                [module]="module" [workshop]="workshop" />
        </ion-list>

        <ion-list *ngIf="submissionInfo && submissionInfo.reviewedby && submissionInfo.reviewedby.length && !assessment">
            <ion-item-divider class="ion-text-wrap">
                <ion-label>
                    <h2>{{ 'addon.mod_workshop.receivedgrades' | translate }}</h2>
                </ion-label>
            </ion-item-divider>
            <ng-container *ngFor="let reviewer of submissionInfo.reviewedby">
                <addon-mod-workshop-assessment *ngIf="!reviewer.ownAssessment" [submission]="submission" [assessment]="reviewer"
                    [courseId]="courseId" [access]="access" [module]="module" [workshop]="workshop" />
            </ng-container>
        </ion-list>

        <ion-list *ngIf="submissionInfo && submissionInfo.reviewerof && submissionInfo.reviewerof.length && !assessment">
            <ion-item-divider class="ion-text-wrap">
                <ion-label>
                    <h2>{{ 'addon.mod_workshop.givengrades' | translate }}</h2>
                </ion-label>
            </ion-item-divider>
            <addon-mod-workshop-assessment *ngFor="let reviewer of submissionInfo.reviewerof" [submission]="submission"
                [assessment]="reviewer" [courseId]="courseId" [module]="module" [workshop]="workshop" [access]="access" />
        </ion-list>

        @if (canAddFeedback && submission) {
            @if (loadFeedbackToEditErrorMessage) {
                <!-- Error loading the feedback to edit it, display a warning. -->
                <ion-item-divider class="ion-text-wrap">
                    <ion-label>
                        <h2>{{ 'addon.mod_workshop.feedbackauthor' | translate }}</h2>
                    </ion-label>
                </ion-item-divider>
                <ion-card class="core-warning-card">
                    <ion-item>
                        <ion-icon name="fas-triangle-exclamation" slot="start" aria-hidden="true" />
                        <ion-label>{{ loadFeedbackToEditErrorMessage }}</ion-label>
                    </ion-item>
                </ion-card>
            } @else {
                <form [formGroup]="feedbackForm" #feedbackFormEl>
                    <ion-item-divider class="ion-text-wrap">
                        <ion-label>
                            <h2>{{ 'addon.mod_workshop.feedbackauthor' | translate }}</h2>
                        </ion-label>
                    </ion-item-divider>
                    <ion-item class="ion-text-wrap" *ngIf="access.canpublishsubmissions">
                        <ion-toggle formControlName="published">
                            <p class="item-heading">{{ 'addon.mod_workshop.publishsubmission' | translate }}</p>
                            <p>{{ 'addon.mod_workshop.publishsubmission_help' | translate }}</p>
                        </ion-toggle>
                    </ion-item>

                    <ion-item-divider class="ion-text-wrap">
                        <ion-label>
                            <h3>{{ 'addon.mod_workshop.gradecalculated' | translate }}</h3>
                            <p>{{ submission.grade }}</p>
                        </ion-label>
                    </ion-item-divider>
                    <ion-item class="ion-text-wrap">
                        <ion-select labelPlacement="stacked" formControlName="grade" interface="action-sheet"
                            [cancelText]="'core.cancel' | translate"
                            [interfaceOptions]="{header: 'addon.mod_workshop.gradeover' | translate}"
                            [label]="'addon.mod_workshop.gradeover' | translate">
                            <ion-select-option *ngFor="let grade of evaluationGrades" [value]="grade.value">
                                {{grade.label}}
                            </ion-select-option>
                        </ion-select>
                    </ion-item>
                    <ion-item>
                        <ion-label position="stacked">{{ 'addon.mod_workshop.feedbackauthor' | translate }}</ion-label>
                        <core-rich-text-editor [control]="feedbackForm.controls['text']" name="text" [autoSave]="true" contextLevel="module"
                            [contextInstanceId]="module.id" elementId="feedbackauthor_editor" [draftExtraParams]="{id: submissionId}" />
                    </ion-item>
                </form>
            }
        }

        <addon-mod-workshop-assessment-strategy *ngIf="assessmentId" [workshop]="workshop" [access]="access" [assessmentId]="assessmentId"
            [userId]="assessmentUserId" [strategy]="strategy" [edit]="access.assessingallowed" />

        <ion-list *ngIf="assessmentId && !access.assessingallowed && assessment?.feedbackreviewer">
            <ion-item class="ion-text-wrap">
                <core-user-avatar *ngIf="evaluateGradingByProfile" [user]="evaluateGradingByProfile" slot="start" [courseId]="courseId"
                    [userId]="evaluateGradingByProfile.id" />
                <ion-label>
                    <h3 class="item-heading" *ngIf="evaluateGradingByProfile && evaluateGradingByProfile.fullname">
                        {{ 'addon.mod_workshop.feedbackby' | translate : {$a: evaluateGradingByProfile.fullname} }}
                    </h3>
                    <core-format-text [text]="assessment!.feedbackreviewer" contextLevel="module" [contextInstanceId]="module.id"
                        [courseId]="courseId" />
                </ion-label>
            </ion-item>
        </ion-list>
    </core-loading>
</ion-content>
